<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>1-发ajax请求</title>
</head>
<body>
  <button id="getBtn">GET AJAX</button>
  <button id="postBtn">POST AJAX</button>
  <button id="putBtn">PUT AJAX</button>
  <button id="deleteBtn">Delete AJAX</button>
</body>
<script>
  let getBtn = document.getElementById('getBtn');
  let postBtn = document.getElementById('postBtn');
  let putBtn = document.getElementById('putBtn');
  let deleteBtn = document.getElementById('deleteBtn');

  getBtn.onclick = () => {
    // 1. 创建xhr对象
    let xhr = new XMLHttpRequest();
     //2. 指定请求头信息 => 初始化请求     还没有发请求
    xhr.open('get', 'http://localhost:8081/server/2?name=zhang&age=12', true);
    // 3. 发送异步ajax请求(也是http)
    xhr.send();// get请求不能携带请求体, 所有没有参数
  }
  postBtn.onclick = () => {
    let xhr = new XMLHttpRequest();
    xhr.open('post', 'http://localhost:8081/server/3?name=zhang2&age=13', true);
    xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded;charser=utf-8');
    // xhr.setRequestHeader('Content-Type','application/json;charser=utf-8');
    xhr.send('a=1&b=2');
    // xhr.send('{"name": "张三", "age": 11}');
  }
  putBtn.onclick = () => {
    let xhr = new XMLHttpRequest();
    xhr.open('put', 'http://localhost:8081/server/4?name=zhang3&age=14', true);
    xhr.setRequestHeader('Content-Type','application/json;charser=utf-8');
    xhr.send('{"name": "张三2", "age": 12}');
  }
  deleteBtn.onclick = () => {
    let xhr = new XMLHttpRequest();
    xhr.open('delete', 'http://localhost:8081/server/5?name=zhang4&age=15', true);
    xhr.setRequestHeader('Content-Type','application/json;charser=utf-8');
    xhr.send('{"name": "张三3", "age": 13}');
  }
</script>
</html>